package cn.mastor.org.service.impl;

import cn.mastor.basic.domain.PhoneRegisterForm;
import cn.mastor.basic.util.AjaxResult;
import cn.mastor.org.service.IVerifyCodeService;
import cn.mastor.basic.util.StrUtils;
import cn.mastor.basic.util.VerifyCodeUtils;
//import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class IVerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String getImageCode(String uuidKey) {
//        System.out.println(uuidKey);
        String random = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(uuidKey,random,4, TimeUnit.MINUTES);
        return VerifyCodeUtils.VerifyCode(100,35,random);
    }
    @Override
    public String getImageCode2(String uuidKey) {
        System.out.println(uuidKey);
        String random = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(uuidKey,random,4, TimeUnit.MINUTES);
        return VerifyCodeUtils.VerifyCode(100,35,random);
    }

    @Override
    public String getMailCode(PhoneRegisterForm emp) {
//        System.out.println(code);
        System.out.println(emp.getImageCodeKey());
        System.out.println(emp.getImageCodeValue());
        if(emp.getImageCodeValue().equalsIgnoreCase((String) redisTemplate.opsForValue().get(emp.getImageCodeKey()))){
            String code = StrUtils.getComplexRandomString(4);
            String codeKey = emp.getType()+emp.getPhone();
//            System.out.println("创建的"+codeKey);
            redisTemplate.opsForValue().set(codeKey,code,4, TimeUnit.MINUTES);
//            System.out.println("生成的"+redisTemplate.opsForValue().get(codeKey));
            return code;
        }
        return null;
    }
    @Override
    public String getEMailCode(PhoneRegisterForm emp) {
//        System.out.println(code);
        System.out.println(emp.getImageCodeKey());
//        System.out.println(emp.getImageCodeValue());
        System.out.println(redisTemplate.opsForValue().get(emp.getImageCodeKey()));
        if(emp.getImageCodeValue().equalsIgnoreCase((String) redisTemplate.opsForValue().get(emp.getImageCodeKey()))){
            String code = StrUtils.getComplexRandomString(4);
            String codeKey = emp.getType()+emp.getPhone();
//            System.out.println("创建的"+codeKey);
            redisTemplate.opsForValue().set(codeKey,code,4, TimeUnit.MINUTES);
//            System.out.println("生成的"+redisTemplate.opsForValue().get(codeKey));
            return code;
        }
        return null;
    }


}
